// Set 无序且唯一
const arr = [1, 1, 2, 2];
// 去重
const arr2 = [...new Set(arr)];
console.log("arr2: ", arr2);

// 判断元素是否在集合中
const set = new Set(arr);
const has = set.has(2);
console.log("has: ", has);

// 求交集
const set2 = new Set([2, 3]);
const set3 = new Set([...set].filter((item) => set2.has(item)));
console.log("set3: ", set3);

// =========== Set常见操作
let mySet = new Set();
// 添加
mySet.add(1);
mySet.add(3);
mySet.add(5);
mySet.add(5);
console.log("mySet: ", mySet);
// 删除
mySet.delete(5);
// 是否存在
const mySetHas = mySet.has(5);
console.log("mySetHas: ", mySetHas);
// 迭代
for (const item of mySet) {
  console.log(item);
}
for (let key of mySet.keys()) console.log(key);
for (let key of mySet.values()) console.log(key);
for (let [key, value] of mySet.entries()) console.log(key, value);

// Set 和 array互转
let myArr = [...mySet];
myArr = Array.from(mySet);
const mySet2 = new Set([1, 2, 3]);

// 交集和差集
const difference = new Set([...mySet].filter((x) => mySet2.has(x)));
const difference = new Set([...mySet].filter((x) => !mySet2.has(x)));
